﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Jlvier.TaobaoShowcase.Service;

namespace ShowWindowIntelligence
{
    public partial class ProductSelectionForm : Form
    {
        public ProductSelectionForm()
        {
            InitializeComponent();
            InitializeControls();
        }

        public List<Product> Selection
        {
            get { return null;}
        }

        private void InitializeControls()
        {
            lazyInitializer.Controls.Add(gridViewProduct);
            lazyInitializer.Controls.Add(buttonOK);
            lazyInitializer.Controls.Add(buttonSearch);
            lazyInitializer.InitializeControl += LoadAllProducts;
        }

        private void LoadAllProducts(object sender, EventArgs e)
        {

        }

        private object[] CreateValues(Product product)
        {
            TimeSpan remainTime = product.DelistTime.Subtract(DateTime.Now);
            string remainTimeString = string.Format("{0} days {1} hours {2} minutes", 
                                                    remainTime.Days, remainTime.Hours,remainTime.Minutes);
            return new object[] {false,null,product.Title, 
                                 product.DelistTime,
                                 remainTimeString,
                                 product.Price,
                                 product.HasShowcase};
        }

        private void Execute(ThreadStart action)
        {
            if (InvokeRequired)
            {
                Invoke(action);
            }
            else
            {
                action();
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            lazyInitializer.Start();
        }

        private void OnSelectAllCheckedChanged(object sender, EventArgs e)
        {
            foreach(DataGridViewRow row in gridViewProduct.Rows)
            {
                row.Cells[0].Value = checkBoxSelectAll.Checked;
            }
        }
    }
}
